//
//  NJWeiboCell.m
//  iOS
//
//  Created by 李坤 on 16/12/7.
//
//

#import "NJWeiboCell.h"
#import "NJWeibo.h"
#import "NJWeiboFrame.h"

#define NJNameFont [UIFont systemFontOfSize:15]
#define NJTextFont [UIFont systemFontOfSize:16]
@interface NJWeiboCell ()

/**
 *  头像
 */
@property(nonatomic,weak)UIImageView *iconView;
/**
 *  vip
 */
@property(nonatomic,weak)UIImageView *vipView;
/**
 *  配图
 */
@property(nonatomic,weak)UIImageView *pictureView;
/**
 *  昵称
 */
@property(nonatomic,weak)UILabel *nameLabel;
/**
 *  正文
 */
@property(nonatomic,weak)UILabel *introLabel;

@end

@implementation NJWeiboCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"status";
    //1.缓存中取
    NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell){
        cell = [[NJWeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        //让自定义的cell和系统的cell一样，一创建出来来拥有一些子控件提供给我们使用
        //1.创建头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        //2.创建昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = NJNameFont;
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        //3.创建vip
        UIImageView *vipView = [[UIImageView alloc] init];
        vipView.image = [UIImage imageNamed:@"vip"];
        [self.contentView addSubview:vipView];
        self.vipView = vipView;
        
        //4.创建正文
        UILabel *introLabel = [[UILabel alloc] init];
        introLabel.font = NJTextFont;
        introLabel.numberOfLines = 0;
        [self.contentView addSubview:introLabel];
        self.introLabel = introLabel;
        
        //5.创建配图
        UIImageView *pictureView = [[UIImageView alloc] init];
        [self.contentView addSubview:pictureView];
        self.pictureView = pictureView;
    }
    return self;
}

- (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame{
    _weiboFrame = weiboFrame;
    //1.给子控件赋值数据
    [self settingData];
    //2.设置frame
    [self settingFrame];
}

/**
 *  设置子控件的数据
 */
- (void)settingData{
    NJWeibo *weibo = self.weiboFrame.weibo;
    //设置头像
    self.iconView.image = [UIImage imageNamed:weibo.icon];
    //设置昵称
    self.nameLabel.text = weibo.name;
    //设置vip
    if(weibo.vip){
        self.vipView.hidden = NO;
        self.nameLabel.textColor = [UIColor redColor];
    }else{
        self.vipView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }
    //设置内容
    self.introLabel.text = weibo.text;
    //设置配图
    if(weibo.picture){
        //有配图
        self.pictureView.image = [UIImage imageNamed:weibo.picture];
        self.pictureView.hidden = NO;
    }else{
        self.pictureView.hidden = YES;
    }
}

/**
 *  设置子控件的frame
 */
- (void)settingFrame{
    //设置头像的frame
    self.iconView.frame = self.weiboFrame.iconF;
    self.iconView.layer.cornerRadius = self.iconView.bounds.size.width/2;
    
    //设置昵称的frame
    self.nameLabel.frame = self.weiboFrame.nameF;
    
    //设置vip的frame
    self.vipView.frame = self.weiboFrame.vipF;
    
    //设置正文的frame
    self.introLabel.frame = self.weiboFrame.introF;
    
    //设置配图的frame
    if(self.weiboFrame.weibo.picture){//有配图
        self.pictureView.frame = self.weiboFrame.pictureF;
        self.pictureView.clipsToBounds = YES;
        self.pictureView.contentMode = UIViewContentModeCenter;
    }
}

/**
 *  计算文本的宽高
 *
 *  @param str     需要计算的文本
 *  @param font    文本显示的字体
 *  @param maxSize 文本显示的范围
 *
 *  @return 文本占用的真实宽度
 */
- (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize{
    NSDictionary *dict = @{NSFontAttributeName : font};
    //如果将来计算的文字的范围超出了指定的范围，返回的就是指定的范围
    //如果将来计算的文字的范围小鱼指定的范围，返回的就是真实的范围
    CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size;
}

@end
